import axios from 'axios'
import qs from 'qs'
import constant from '@/config/constant.js'
import store from '@/store/index.js'
const mapUtil = {
  AK: constant.AK,
  resolutions: [
    156543.03, 78271.52, 39135.76, 9783.94, 4891.97,
    2445.98, 1222.99, 611.50, 305.75, 152.87,
    76.44, 38.22, 19.11, 9.55, 4.78,
    2.39, 1.19, 0.597, 0.299, 0.149,
    0.075, 0.037, 0.019
  ],
  init() {

  },
  showPanel() {
    store.state.panelVisible = true;
  },
  hidePanel() {
    store.state.panelVisible = false;
  },
  isBusy() {
    return store.state.waiting || store.state.drawing;
  },
  startBusy() {
    store.state.waiting = true;
    store.state.drawing = true;
  },
  stopBusy() {
    store.state.waiting = false;
    store.state.drawing = false;
  },
  showLoading() {
    store.state.waiting = true;
  },
  hideLoading() {
    store.state.waiting = false;
  },
  showDrawing() {
    store.state.drawing = true;
  },
  hideDrawing() {
    store.state.drawing = false;
  },
  getMap() {
    return store.state.map;
  },
  addGraphic(graphicLayer, features, type, fieldName) {
    for (let i = 0, len = features.length; i < len; i++) {
      let flag = false;
      let graphics = graphicLayer.all();
      let fieldValue = features[i].attrs[fieldName];
      for (let i in graphics) {
        let graphic = graphics[i];
        if (graphic.options.type == type && graphic.attrs[fieldName] == fieldValue) {
          flag = true;
          graphic.remove();
          break;
        }
      }
      if (!flag) {
        let graphic;
        if (features[i].geom.m) {
          graphic = new G.Graphic.MultiPolygon(features[i].geom, {
            [fieldName]: fieldValue
          }, constant.GRAPHIC_POLYGON_STYLE)
        } else {
          graphic = new G.Graphic.Polygon(features[i].geom, {
            [fieldName]: fieldValue
          }, constant.GRAPHIC_POLYGON_STYLE);
        }
        graphic.options.type = type;
        // graphic.tnid = tnid;
        graphicLayer.add(graphic);
      }
    }
  },
  getExtent(geom) {
    let [xmin, ymin] = [Number.MAX_VALUE, Number.MAX_VALUE];
    let [xmax, ymax] = [Number.MIN_VALUE, Number.MIN_VALUE];
    if (geom.m) {
      geom.m.forEach(polygon => {
        polygon.forEach(ring => {
          ring.forEach(point => {
            if (point[0] <= xmin) {
              xmin = point[0];
            }
            if (point[1] <= ymin) {
              ymin = point[1];
            }
            if (point[0] >= xmax) {
              xmax = point[0];
            }
            if (point[1] >= ymax) {
              ymax = point[1];
            }
          });
        });
      });
    } else {
      geom.forEach(ring => {
        ring.forEach(point => {
          if (point[0] <= xmin) {
            xmin = point[0];
          }
          if (point[1] <= ymin) {
            ymin = point[1];
          }
          if (point[0] >= xmax) {
            xmax = point[0];
          }
          if (point[1] >= ymax) {
            ymax = point[1];
          }
        });
      });
    }
    let extent = [xmin, ymin, xmax, ymax];
    return extent;
  }

};
mapUtil.init();

export default mapUtil;
